1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8
9 package org.webdocwf.util.smime.util;
10
11
12 import org.webdocwf.util.smime.exception.SMIMEException;
13
14
15 /***
16 * ByteArrayComparator is used for finding first appearance of the inner byte
17 * array in the outher byte array.
18 */
19 public class ByteArrayComparator {
20
21 /***
22 * Position of first element from array of elements in the lookUpArray0 which
23 * corresponds to toFind0 array
24 */
25 private int positionFirst = -1;
26
27 /***
28 * Construction with the given byte arrays (first is being searched in the
29 * second (lookup) byte array)
30 * @param toFind0 byte array which is being searhced
31 * @param lookUpArray0 byte array to be searched
32 * @exception SMIMEException if toFind0 array is bigger than or equal to
33 * lookUpArray0 array!
34 */
35 public ByteArrayComparator(byte[] toFind0, byte[] lookUpArray0) throws SMIMEException {
36 if (toFind0.length >= lookUpArray0.length)
37 throw new SMIMEException(this, 1033);
38 positionFirst = find(toFind0, lookUpArray0);
39 }
40
41 /***
42 * Finds first appearance of the byte array in the outher byte array
43 * @param toFind0 byte array which is being searched
44 * @param lookUpArray0 byte array to be searched
45 * @return Position of the first matching byte array
46 */
47 private int find(byte[] toFind0, byte[] lookUpArray0) {
48 int k;
49 int positionFirst = -1;
50 byte[] prStr;
51
52 for (k = 0; k != lookUpArray0.length; k++) {
53 for (int z = 0; z != toFind0.length; z++) {
54 positionFirst = k;
55 if (toFind0[z] != lookUpArray0[k + z]) {
56 positionFirst = -1;
57 break;
58 }
59 }
60 if (positionFirst == k)
61 break;
62 }
63 return positionFirst; // -1 if no matching found
64 }
65
66 /***
67 * Returns the position of the first matching byte in the outher byte array
68 * @return Position of the first matching byte in the outher byte array
69 */
70 public int getMatchingIndex() {
71 return positionFirst;
72 }
73 }
74
This page was automatically generated by Maven